package utils

import (
	"math/rand"
	"time"
)

// 时间转换的模板，golang里面只能是 "2006-01-02 15:04:05" （go的诞生时间）
var TIME_LAYOUT string = "2006-01-02 15:04:05"

//  字符串转时间
func LayoutToTime(value string) (t time.Time, err error) {
	t, err = time.ParseInLocation(TIME_LAYOUT, value, time.Local)
	return
}

//  时间转字符串
func TimeToLayout(t time.Time) (layout string) {
	layout = t.Format(TIME_LAYOUT)
	return
}

// Unix时间戳转时间
func UnixToTime(sec int64, nsec int64) time.Time {
	return time.Unix(sec, nsec)
}

var byteLen = 62
var byteLst = []byte{
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
}

// 生成n位的兑换码
func ExchangeCode(n int) string {
	lst := make([]byte, 0)
	for i := 0; i < n; i++ {
		lst = append(lst, byteLst[rand.Intn(byteLen)])
	}
	return string(lst)
}
